<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\Redis;
use App\Http\Middleware\WordFilteringMiddleware;
use App\Http\Model\Room;
use App\Http\Model\User;
use App\Http\Model\Trends;

class HandelContentWord extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'handelContentWord {param}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '处理内容关键词';

    protected $wordFilteringMiddleware;

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
        $this->wordFilteringMiddleware = new WordFilteringMiddleware();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {

        $param = $this->argument('param');
        if (!$param) return false;

        if ($param == "user") {
            $this->handleUser();
        }

        if ($param == "room") {
            $this->handleRoom();
        }

        if ($param == "trend_content") {
            $this->handleTrendContent();
        }

        if ($param == "trend_comment") {
            $this->handleTrendComment();
        }



    }

    protected function handleUser()
    {
        //获取
        $offset = 0;
        $limit = 100;
        do
        {
            $userModel = new User();
            $userDataList = $userModel->getHandleRoomList($offset, $limit);
            $result = $this->wordFilteringMiddleware->handle($userDataList);
            if ($result) {
                foreach ($result as $key => $value) {
                    echo $value['handle']['data']['user_id'] . PHP_EOL;
                    Redis::HSET("HANDEL:CONTENT:WORD:USER:LIST", $value['handle']['data']['user_id'], json_encode($value));
                }
            }

            $offset = $offset + $limit;
            sleep(1);
        }
        while ($userDataList);
    }

    protected function handleRoom()
    {
        //获取
        $offset = 0;
        $limit = 100;
        do
        {
            $roomModel = new Room();
            $roomDataList = $roomModel->getHandleRoomList($offset, $limit);
            $result = $this->wordFilteringMiddleware->handle($roomDataList);
            if ($result) {
                foreach ($result as $key => $value) {
                    echo $value['handle']['data']['room_id'] . PHP_EOL;
                    Redis::HSET("HANDEL:CONTENT:WORD:ROOM:LIST", $value['handle']['data']['room_id'], json_encode($value));
                }
            }

            $offset = $offset + $limit;
            sleep(1);
        }
        while ($roomDataList);
    }


    protected function handleTrendContent()
    {
        //获取
        $offset = 0;
        $limit = 50;
        do
        {
            $trendModel = new Trends();
            $trendContentDataList = $trendModel->getHandleTrendContentList($offset, $limit);
            $result = $this->wordFilteringMiddleware->handle($trendContentDataList);
            if ($result) {
                foreach ($result as $key => $value) {
                    echo $value['handle']['data']['content_id'] . PHP_EOL;
                    Redis::HSET("HANDEL:CONTENT:WORD:TREND:CONTENT:LIST", $value['handle']['data']['content_id'], json_encode($value));
                }
            }

            $offset = $offset + $limit;
            sleep(1);
        }
        while ($trendContentDataList);
    }

    protected function handleTrendComment()
    {
        //获取
        $offset = 0;
        $limit = 50;
        do
        {
            $trendModel = new Trends();
            $trendCommentDataList = $trendModel->getHandleTrendCommentList($offset, $limit);
            $result = $this->wordFilteringMiddleware->handle($trendCommentDataList);
            if ($result) {
                foreach ($result as $key => $value) {
                    echo $value['handle']['data']['comment_id'] . PHP_EOL;
                    Redis::HSET("HANDEL:CONTENT:WORD:TREND:COMMENT:LIST", $value['handle']['data']['comment_id'], json_encode($value));
                }
            }

            $offset = $offset + $limit;
            sleep(1);
        }
        while ($trendCommentDataList);
    }
}
